import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import 'font-awesome/css/font-awesome.css'

import {postRequest} from "./network/api";
import {putRequest} from "./network/api"
import {getRequest} from "./network/api";
import {deleteRequest} from "./network/api";
import {initMenu} from "./network/menus";
import {downloadRequest} from "./network/download";

Vue.use(ElementUI,{size: 'small'})
Vue.config.productionTip = false

Vue.prototype.$postRequest = postRequest;
Vue.prototype.$getRequest = getRequest;
Vue.prototype.$putRequest = putRequest;
Vue.prototype.$deleteRequest = deleteRequest;
Vue.prototype.$downloadRequest = downloadRequest;


router.beforeEach((to, from, next)=>{
  if(window.sessionStorage.getItem('tokenStr')) {
    initMenu(router, store);
    if(!window.sessionStorage.getItem('user')){
      //判断用户信息石否存在
      return getRequest('/admin/info').then(res=>{
        if(res){
          //存入信息
          window.sessionStorage.setItem('user',JSON.stringify(res));
          store.commit('initAdmin',res);
          next();
        }
      })
    }
    next();
  } else{
    if(to.path == '/' || to.path == '/error'){
      next();
    } else{
      next('/?redirect='+to.path);
    }
    next();
  }
})


new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
